const Actions = require('./actions');

const DB = require('app/db');
module.exports = Reflux.createStore({
    listenables: [Actions],
    data: {
        listData: []
    },

    onFetch: function (params, cb) {
        let t = this;
        DB.Approval.get_approval_detail({ process_id: params.process_id })
            .then((content) => {
                // {subject:"方案评审",content:[
                // {title:"IT项目界面设计",date:"2017.01.10",badge_text:"已通过"},
                // {title:"IT项目界面开发",date:"2017.01.10",badge_text:"未通过"},
                // {title:"IT项目界面设计",date:"2017.01.10",badge_text:"未通过"},
                // {title:"IT项目界面设计",date:"2017.01.10",badge_text:"未通过"},
                // {title:"IT项目需求规格",date:"2017.01.10",badge_text:"已通过",times:true}]}
                let nodelists = content.form_component_values.nodelists;
                let array = [];
                let content_arr = [];
                let obj = {};
                let sub_obj = {};
                for (let i = 0; i < nodelists.length; i++) {
                    if (nodelists[i].node_achievements.length > 0) {
                        let node = nodelists[i].node_achievements;
                        let n = 0;//是否超时
                        for (let j = 0; j < node.length; j++) {
                            let time = node[j].time.replace(/\//g, ".");
                            sub_obj = { title: node[j].achievements_name, date: time, badge_text: (node[j].status != 2 ? "未通过" : "已通过") }
                            if (node[j].status == 1) {                             
                                if(n > 1){
                                    sub_obj.times = true;
                                }
                                ++n;
                            }
                            if(node[j].status == 0){
                                sub_obj.times = true;
                            }
                            content_arr.push(sub_obj);
                        }
                        obj.subject = `${nodelists[i].node_name}`;
                        obj.content = content_arr;
                        array.push(obj);
                    }
                    obj = {};
                    sub_obj = {};
                    content_arr = [];
                }
                this.data.listData = array;
                t.updateComponent();
            })
            .catch((error) => {
                cb && cb(t.data);
            });
    },

    updateComponent: function () {

        this.trigger(this.data);

    }
});
